//
//  TalkfunDownloadManager.h
//  TalkfunDownloadManager
//
//  Created by 孙兆能 on 16/7/22.
//  Copyright © 2016年 talk-fun. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TalkfunSDK.h"
NS_ASSUME_NONNULL_BEGIN
typedef enum {
    TalkfunDownloadStatusUnstart    = 0, //未开始
    TalkfunDownloadStatusPrepare    = 1, //等待中
    TalkfunDownloadStatusStart      = 2, //正在下载
    TalkfunDownloadStatusPause      = 3, //暂停
    TalkfunDownloadStatusFinish     = 4, //下载完成
    TalkfunDownloadStatusError      = 5 , //下载错误
    TalkfunDownloadStatusExpired    = 6  //资源过期
}TalkfunDownloadStatus;

/**下载进度的模型*/
@interface TalkfunDownloadProgressModel : NSObject
@property(nonatomic,strong)NSString* _Nonnull playbackID;//课程id
@property(nonatomic,assign)CGFloat         totalSize;//课程总大小
@property(nonatomic,assign)CGFloat         downloadedSize;//已下载的大小
@end

/**下载时的状态的模型*/
@interface TalkfunDownloadStatusModel : NSObject
@property(nonatomic,strong)NSString* _Nonnull   playbackID;//课程id
@property(nonatomic,assign)CGFloat   downloadedSize;//已下载的大小
@property(nonatomic,assign)CGFloat   duration;      //课程长度
@property(nonatomic,assign)CGFloat   totalSize;      ///课程总大小
@property(nonatomic,assign)TalkfunDownloadStatus downloadStatus;
@property (nonatomic, copy) NSString * _Nonnull thumb;
@end
/**下载发生错误的模型*/
@interface TalkfuError : NSObject
@property(nonatomic,strong)NSString *_Nonnull playbackID;//课程id
@property(nonatomic,strong)NSString *_Nonnull reasonDescription;//原因描述
@property(nonatomic,assign)NSString *_Nonnull statusCode;//状态码
@property(nonatomic,assign)NSString *_Nonnull URL;
@end

//课程的模型
@interface TalkfuDownloadModel : NSObject
/**课程总大小*/
@property (nonatomic,assign)CGFloat totalSize;
/**已下载的大小*/
@property (nonatomic,assign)CGFloat downloadedSize;
/**课程总时长*/
@property (nonatomic,assign)CGFloat duration;
/**未开始  、 暂停  、 正在下载  、 下载完成 、 下载错误   status*/
@property (nonatomic,assign)TalkfunDownloadStatus downloadStatus;
/**访问令牌*/
@property (nonatomic,copy)NSString * access_token;
/**课程id*/
@property (nonatomic,copy)NSString * playbackID;
/**课程标题*/
@property (nonatomic,copy)NSString * title;
/**课程的缩略图*/
@property (nonatomic,copy)NSString * thumb;

@end

//代理
@protocol TalkfunDownloadManagerDelegate <NSObject>
@optional
/**下载时的状态*/
- (void)downloadStatus:(TalkfunDownloadStatusModel*)statusModel;
/**下载进度*/
- (void)downloadProgress:(TalkfunDownloadProgressModel*)progressModel ;
/**下载发生错误*/
- (void)downloadError:(TalkfuError *)error;
@end

typedef void (^BGCompletedHandler)(void);
typedef void (^ TalkfunDiskLowReminderValueCallback)(void);

/**
 *  缓存路径
 */
typedef enum {
    
    TalkfunCachePathDocuments = 0,
    TalkfunCachePathLibrary   = 1
    
}TalkfunCachePath;

@interface TalkfunDownloadManager : NSObject

@property (nonatomic, copy) BGCompletedHandler  completedHandler;

//下载线程数(最大为3）
@property (nonatomic,assign) NSInteger downloadThreadNumber;
@property (nonatomic,assign) TalkfunCachePath cachePath;

@property (nonatomic, weak) id<TalkfunDownloadManagerDelegate> delegate;

//警告磁盘空间不足
@property (nonatomic, copy) TalkfunDiskLowReminderValueCallback  diskLowReminderValueCallback;
/**
 *  默认值是1024M (最小值为1024)
 *  磁盘空间小于这个阀值,会停止下载,   diskLowReminderValueCallback弹窗提示
 */
@property(nonatomic,assign)NSInteger diskLowReminderValue;
/**
 *  单例
 */
+ (TalkfunDownloadManager*)shareManager;
/**
// *  获取下载的课程列表模型 2.0
// */
- (NSArray <TalkfuDownloadModel *>* _Nonnull)getDownloadModelList;
/**
 *  开关日志输出
 */
- (void)setLogEnable:(BOOL)enable;
/**
 *  配置日志输出等级
 */
- (void)setLogLevel:(TalkfunLoggerLevel)level;
/**
 *  下载某个点播
 *
 *   access_token是点播的access_token（不可为空）
 *   placbackID是点播对应的ID（不可为空,用于区分离线数据）
 *   title是显示在下载列表中的title（可传nil,显示playbackID）
 */
- (void)appendDownloadWithAccessToken:(nonnull NSString *)access_token playbackID:(nonnull NSString *)playbackID title:(nonnull NSString *)title;
/**
 *  下载某个点播
 *   accessKey不可为空
 *   placbackID是点播对应的ID（不可为空,用于区分离线数据）
 *   title是显示在下载列表中的title（可传nil,显示playbackID）
 */
- (void)appendDownloadWithAccessKey:(nonnull NSString *)accessKey playbackID:(nonnull NSString *)playbackID title:(nonnull NSString *)title;

/**
 *  全部下载任务暂停
 */
- (void)pauseAllDownload;
/**
 *  开始对应的下载任务
 *
 *  playbackID是对应的任务
 */
- (void)startDownload:( NSString *)playbackID;
/**
 *  暂停对应的下载任务
 *
 *  playbackID是对应的任务
 */
- (void)pauseDownload:( NSString *)playbackID;
/**
 *  删除对应的点播
 *
 *  删除是否成功的信息在callback中
 */
- (void)deleteDownload:( NSString *)playbackID success:(void (^ _Nullable)(id _Nullable))successBlock;
/**
 *  监听事件，处理回调数据
 *
 *  监听事件，event为事件名，获取的数据在回调的callback里面
 **/
- (void)on:(NSString * _Nonnull)event callback:(void (^_Nonnull)(id _Nonnull result))callback;

/**
 *  监听事件，处理回调数据
 *
 *  监听事件，event为事件名，获取的数据在回调的callback里面
 **/
- (void)on:(NSString *_Nonnull)event withCallback:(void (^_Nonnull)(id _Nonnull result))callback NS_DEPRECATED(2_0, 2_0, 2_0, 2_0, "该方法已过期");
/**
 *  判断下载列表是否有对应playbackID
 **/
- (BOOL)containsPlaybackID:(nonnull NSString *)playbackID;
/**
 *  按照playbackID获取对应信息
 **/
- (nullable id)getInfoWithPlaybackID:(nonnull NSString *)playbackID;

/**
 *  这个课程是否下载完成
 **/
- (BOOL)isFinishDownload:(NSString *_Nonnull)playbackID;

//回放视频类型。1：普通大班回放，2：纯视频类型回放
- (NSString*_Nonnull)getVideoType:(NSString*_Nonnull)playbackID;
/**
 *  获取下载的课程列表 1.0
 */
- (NSArray * _Nonnull)getDownloadList;
/**
 *  根据状态获取相应的下载列表
 */
- ( NSArray * _Nonnull)getDownloadListWithStatus:(TalkfunDownloadStatus)downloadStatus;
@end
NS_ASSUME_NONNULL_END
